package cn.leecode.q14;

/**
 * 编写一个函数来查找字符串数组中的最长公共前缀。
 * 如果不存在公共前缀，返回空字符串 ""。
 * 示例 1：
 * 输入：strs = ["flower","flow","flight"]
 * 输出："fl"
 * 示例 2：
 * 输入：strs = ["dog","racecar","car"]
 * 输出：""
 * 解释：输入不存在公共前缀。
 * 提示：
 * 1 <= strs.length <= 200
 * 0 <= strs[i].length <= 200
 * strs[i] 仅由小写英文字母组成
 */
public class PublicPrefix {
    public String longestCommonPrefix(String[] strs) {
        String prefix = "";
        for (int i = 0; i < strs[0].length(); i++) {
            char c = strs[0].charAt(i);
            for (int j = 0; j < strs.length; j++) {
                if (i >= strs[j].length() || strs[j].charAt(i) != c) {
                    return prefix;
                }
            }
            prefix += c;
        }
        return prefix;
    }

    public static void main(String[] args) {
        //获取当前时间毫秒
        long startTime = System.currentTimeMillis();
        PublicPrefix publicPrefix = new PublicPrefix();
//        String result = publicPrefix.longestCommonPrefix(new String[]{"flower","flow","flight"});
        String result = publicPrefix.longestCommonPrefix(new String[]{"dog","racecar","car"});
        System.out.println("结果：" + result + ",耗时" + (System.currentTimeMillis() - startTime) + "ms");
    }
}
